<?php
abstract class BaseController extends Yaf_Controller_Abstract{
	protected $_layout;

	function init(){
	}

	#设置信息
	function set_popup_message($message){
		Yaf_Session::getInstance()->set('popup_message',$message);
	}

	#提取信息
	function get_popup_message(){
		#根据yaf自带的函数获取弹出的消息
		$message = Yaf_Session::getInstance()->get('popup_message');
		if(empty($message)){
			return '';
		}else{
			#删除消息缓存
			Yaf_Session::getInstance()->del('popup_message');
			return $message;
		}
	}


	#写入日志
	function _log_result($str, $file_name = 'log.txt') {
		//目录
		$dir = "/data/bak/log/";
		#打开文件
		$fp = fopen($dir . $file_name,"a");
		#锁定，不允许其他人修改
		flock($fp, LOCK_EX) ;
		#写入
		fwrite($fp,"执行日期：".strftime("%Y%m%d%H%M%S",time()).": \n".$str."\n");
		#解锁
		flock($fp, LOCK_UN);
		#完毕
		fclose($fp);
	}


    function b64encode($str)
    {
        $info = base64_encode($str);
        $info = str_replace(array('+', '/', '='), array('-', '_', ''), $info);
        return $info;
    }



    function encrypt($info, $keysercet)
    {
        $char = $str = '';
        $keysercet = md5($keysercet);
        $x = 0;
        $len = strlen($info);
        $l = strlen($keysercet);
        for ($i = 0; $i < $len; $i++) {
            if ($x == $l) {
                $x = 0;
            }
            $char .= $keysercet{$x};
            $x++;
        }
        for ($i = 0; $i < $len; $i++) {
            $str .= chr(ord($info{$i}) + (ord($char{$i})) % 256);
        }
        return $this->b64encode($str);
    }

    function b64decode($string)
    {
        $url = str_replace(array('-', '_'), array('+', '/'), $string);
        $get_mod4 = strlen($url) % 4;
        if ($get_mod4) {
            $url .= substr('====', $get_mod4);
        }
        return base64_decode($url);
    }


    function decrypt($params, $keysercet)
    {
        $char_str = $string = '';
        $x = 0;
        $keysercet = md5($keysercet);
        $params = $this->b64decode($params);
        $len = strlen($params);
        $l = strlen($keysercet);
        for ($i = 0; $i < $len; $i++) {
            if ($x == $l) {
                $x = 0;
            }
            $char_str .= substr($keysercet, $x, 1);
            $x++;
        }
        for ($i = 0; $i < $len; $i++) {
            if (ord(substr($params, $i, 1)) < ord(substr($char_str, $i, 1))) {
                $string .= chr((ord(substr($params, $i, 1)) + 256) - ord(substr($char_str, $i, 1)));
            } else {
                $string .= chr(ord(substr($params, $i, 1)) - ord(substr($char_str, $i, 1)));
            }
        }
        return $string;
    }



}